<?php
$data = FALSE;
if (isset($_POST['psid'])) {
    require_once '../Controllers/MenuParticipanteController.php';
    $movimientos = getMovimientos($_POST['psid']);
    $data = getData($_POST['psid']);
    if (!$data) {
        die("No se obtuvieron los datos para generar la vista");
    }
} else {
    die("No se recibieron los parámetros correspondientes");
}
?>

<script type="text/javascript" src="./Fuentes/js/MenuParticipante.js"></script>
<script type="text/javascript" src="../../Resources/js/utils.js"></script>
<div class="modal fade" id="menuParticipante" tabindex="-1" role="dialog" keyboard="true" 
     aria-labelledby="menuParticipanteLabel" aria-hidden="true" data-backdrop="static" >
    <div class="modal-dialog modal-lg ">        
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Cerrar</span></button>
                <h4 class="modal-title" id="menuParticipanteLabel"><span class="glyphicon glyphicon-user"></span><?php echo " " . $data['apenom']; ?></h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-sm-7" id="tablaMovimientos" >
                        <div class='panel panel-primary' >
                            <div class="panel-heading text-center" style="font-weight: bold;">MOVIMIENTOS</div>
                            <div style="height: 420px; overflow-y: auto;">
                                <table class='table table-striped table-hover ' >
                                    <tbody>
                                        <?php
                                        if ($movimientos) {
                                            foreach ($movimientos as $mov) {
                                                echo "<tr>
                                                        <td class='text-left' width='34%' >" . date_format(date_create($mov['fecha_hora']), 'd/m/Y H:i:s') . "</td>
                                                        <td class='text-left' width='66%'>" . $mov['tipo'] . "</td>
                                                      </tr>";
                                            }
                                        }
                                        ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-5">
                        <div class='panel panel-default'>
                            <div class="panel-heading text-center" style="font-weight: bold;">DATOS DEL PARTICIPANTE</div>
                            <table class='table table-striped table-bordered '>
                                <tr>
                                    <td style="font-weight: bold;">ESTADO:</td><td class='text-center'><?php echo $data['estado']; ?></td>
                                    <td style="font-weight: bold;">CODIGO:</td><td class='text-center'><?php echo $data['persona_id']; ?></td>
                                </tr>
                                <tr>
                                    <td style="font-weight: bold;">MESA:</td><td class='text-center'><?php echo $data['mesa']; ?></td>
                                    <td style="font-weight: bold;">POSICION:</td><td class='text-center'><?php echo $data['posicion']; ?></td>
                                </tr>
                                <tr>
                                    <td style="font-weight: bold;">REBUYS:</td>
                                    <td class='text-center'>
                                        <?php
                                        $lim = ($torneo['limite_recompras']==0)?"":"/".$torneo['limite_recompras'];
                                        echo $data['rebuys_realizados'] . $lim;
                                        echo ($data['rebuys_pendientes'] > 0) ? "&nbsp; |&nbsp; $" . $data['rebuys_pendientes'] : "";
                                        ?>
                                    </td>
                                    <td style="font-weight: bold;">ADD ON:</td>
                                    <td class='text-center'>
                                        <?php
                                        echo $data['addons_realizados'];
                                        echo ($data['addons_pendientes'] > 0) ? "&nbsp; |&nbsp; $" . $data['addons_pendientes'] : "";
                                        ?>
                                    </td>
                                </tr>                                
                            </table>
                        </div>
                        <div class='panel panel-default' style="height: 290px;">
                            <div class="panel-body ">
                                <ul class="nav nav-tabs nav-justified">
                                    <li class="active"><a href="#comprar" data-toggle="tab"><span class="glyphicon glyphicon-usd"></span> Comprar</a></li>
                                    <li class="" ><a href="#mover" data-toggle="tab" onclick="$('#mesa').focus();"><span class="glyphicon glyphicon-fullscreen"></span> Mover</a></li>
                                    <li class=""><a href="#remover" data-toggle="tab"><span class="glyphicon glyphicon-remove"></span> Eliminar</a></li>
                                </ul>
                                <div id="myTabContent" class="tab-content">
                                    <div class="tab-pane fade active in" id="comprar">
                                        <div class="panel-body ">
                                            <?php
                                            //REBUYS
                                            $habilitarRebuy = FALSE;
                                            $disabledRebuy = "disabled";
                                            $cant = $data['rebuys_realizados'];
                                            $total = ($torneo['limite_recompras'] > 0) ? " / " . $torneo['limite_recompras'] : "";

                                            //REGLAS PARA HABILITAR EL BOTON REBUY
                                            if ($torneo['nivel_actual'] <= $torneo['nivel_max_recompras']) {
                                                if ($torneo['limite_recompras'] == 0) {
                                                    $habilitarRebuy = TRUE;
                                                } else {
                                                    if ($data['rebuys_realizados'] < $torneo['limite_recompras']) {
                                                        $habilitarRebuy = TRUE;
                                                    }
                                                }
                                            }

                                            if ($habilitarRebuy) {
                                                $disabledRebuy = "";
                                                $cant = $data['rebuys_realizados'] + 1;
                                                //REBUY PREPAGO
                                                if ($data['rebuys_pendientes'] > 0) {
                                                    echo "<div class='checkbox'>
                                                            <label><input id='rebuyPrepago' type='checkbox'> Utilizar Rebuy Prepago</label>
                                                          </div>";
                                                } else {
                                                    //CANJE ADDON
                                                    if ($data['addons_pendientes'] > 0 && $torneo['nivel_actual'] < $torneo['nivel_max_recompras']) {
                                                        echo "<div class='checkbox'>
                                                                <label class='text-warning'>
                                                                  <input id='canjeAddon' type='checkbox'> Utilizar Add On Prepago como Rebuy
                                                                </label>
                                                              </div>";
                                                    } else {
                                                        echo "&nbsp;";
                                                    }
                                                }
                                            } else {
                                                echo "&nbsp;";
                                            }

                                            echo "<button class='btn btn-lg btn-success btn-block' id='btnRebuy' onclick='rebuy(" . $_POST['psid'] . ")' $disabledRebuy>REBUY &nbsp; $cant$total </button>";


                                            //ADDONS
                                            $disabledAddon = "disabled";
                                            if ($torneo['nivel_actual'] == $torneo['nivel_max_recompras'] && $data['addons_realizados']<1) {
                                                if ($data['addons_pendientes'] > 0 && $torneo['limite_recompras'] > 0) {
                                                    echo "<div class='checkbox'>
                                                        <label>
                                                          <input id='addonPrepago' type='checkbox'> Utilizar Add On Prepago 
                                                        </label>
                                                      </div>";
                                                } else {
                                                    echo "&nbsp;";
                                                }
                                                $disabledAddon = "";
                                            } else {
                                                echo "&nbsp;";
                                            }
                                            echo "<button class='btn btn-lg btn-warning btn-block' id='btnAddon' onclick='addon(" . $_POST['psid'] . ")' $disabledAddon>ADD ON</button>";
                                            ?>

                                        </div>
                                    </div>
                                    <div class="tab-pane fade" id="mover" >
                                        <div class="panel-body ">
                                            <form role="form" id="formMoverParticipante" autocomplete="off" >
                                                <input type="hidden" value="<?php echo $_POST['psid']; ?>" id="psid">
                                                <div class="col-sm-12">&nbsp;</div>
                                                <div class="row">
                                                    <div class="col-sm-6">
                                                        <input type="text" class="form-control input-lg text-center numeroEntero" style="font-weight: bold; font-size: 22px;" 
                                                               id="mesa" name="mesa" placeholder="Mesa"  maxlength="2" value="" max="<?php echo $torneo['cantidad_mesas']; ?>">
                                                    </div>
                                                    <div class="col-sm-6">
                                                        <input type="text" class="form-control input-lg text-center numeroEntero" style="font-weight: bold; font-size: 22px;" 
                                                               id="posicion" name="posicion" placeholder="Posición" maxlength="2" value="" max="<?php echo $torneo['jugadores_x_mesa']; ?>">
                                                    </div>
                                                </div>
                                                <div class="col-sm-12">&nbsp;</div>
                                                <button class="btn btn-lg btn-success btn-block" type="submit" id="btnMover" >MOVER PARTICIPANTE</button>
                                            </form>
                                        </div>
                                    </div>
                                    <div class="tab-pane fade" id="remover">
                                        <div class="panel-body ">
                                            <div class="col-sm-12">&nbsp;</div>
                                            <button class="btn btn-lg btn-warning btn-block" id="btnEliminar" onclick="eliminar(<?php echo $_POST['psid'];?>)">ELIMINAR</button>
                                            <div class="col-sm-12">&nbsp;</div>
                                            <button class="btn btn-lg btn-danger btn-block" id="btnDescalificar" onclick="descalificar(<?php echo $_POST['psid'];?>)">DESCALIFICAR</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="text-warning" id="formMensaje">&nbsp;</div>
            </div>
        </div>        
    </div>
</div>
<script type='text/javascript'> $('#menuParticipante').modal('show');</script>
